package com.axis2.common.util;

import java.io.UnsupportedEncodingException;

/**
 * @Description
 * @author xxliu
 * @version 0.0.1
 */
public class Utf8ToGbk {
	/**
	 * 
	 * @Description 
	 * @param gbk
	 * @return
	 */
	public static String gbk2utf8(String gbk) {
		try {
		    String iso = new String(gbk.getBytes("UTF-8"),"ISO-8859-1");   
		    String utf8=new String(iso.getBytes("ISO-8859-1"),"UTF-8");  
			return utf8;
		} catch (UnsupportedEncodingException e) {
			return null;
		} 
    }
 
	/**
	 * UTF8 转GBK
	 * @Description
	 * @param utf
	 * @return
	 */
    public static String utf82gbk(String utf) {
		try {
			String utf8 = new String(utf.getBytes( "UTF-8"));
			String unicode = new String(utf8.getBytes(),"UTF-8");   
	    	String gbk = new String(unicode.getBytes("GBK"),"GBK");
			return gbk;
		} catch (UnsupportedEncodingException e) {
			return null;
		} 
		
    	
    }
 
    public static String getEncoding(String str) {      
        String encode = "GB2312";      
       try {      
           if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s = encode;      
               return s;      
            }      
        } catch (Exception exception) {      
        }      
        encode = "ISO-8859-1";      
       try {      
           if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s1 = encode;      
               return s1;      
            }      
        } catch (Exception exception1) {      
        }      
        encode = "UTF-8";      
       try {      
           if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s2 = encode;      
               return s2;      
            }      
        } catch (Exception exception2) {      
        }      
        encode = "GBK";      
       try {      
           if (str.equals(new String(str.getBytes(encode), encode))) {      
                String s3 = encode;      
               return s3;      
            }      
        } catch (Exception exception3) {      
        }      
       return "";      
    }      
}
